package cn.wolfcode.ssm.util;

import java.util.Arrays;
import java.util.Collection;
import java.util.Map;

public class PropertionUtil {
    /**
     * 获取方法和参数内容
     *
     * @param className 类名 非必填
     * @param mothodName 方法名 必填
     * @param properties 参数名称列表
     * @param agrs 参数列表
     * @return
     */
    public static String getMothodPropertion(String className, String mothodName, String[] properties, Object ... agrs) {
        StringBuilder sb = new StringBuilder();
        if(null != className) {
            sb.append("类名:[").append(className).append("],");
        }
        sb.append("方法:[").append(mothodName).append("]");
        if (null == agrs) {
            return sb.toString();
        }
        boolean flag = null != properties;
        int propertiesLength = properties.length;
        Object obj;
        for(int i = 0, length = agrs.length; i < length; i++) {
            obj = agrs[i];
            sb.append("\r\n参数索引:[").append(i).append("],");
            if (flag && i < propertiesLength) {
                sb.append("参数名称:[").append(properties[i]).append("],");
            }
            if(null == obj) {
                sb.append("为null");
                continue;
            }
            sb.append("类型:[").append(obj.getClass().getName()).append("],");
            if(obj instanceof Collection) {
                Collection collection = (Collection)obj;
                sb.append("长度:[").append(collection.size()).append("],内容:[").append(collection);
            } else if(obj instanceof Map) {
                Map map = (Map)obj;
                sb.append("长度:[").append(map.size()).append("],内容:[").append(map);
            } else if(obj instanceof Object[]) {
                Object[] objects = (Object[])obj;
                sb.append("长度:[").append(objects.length).append("],内容:[").append(Arrays.asList(objects));
            } else if(obj instanceof String) {
                sb.append("内容:[").append(obj);
            } else {
                sb.append("内容:[").append(String.valueOf(obj));
            }
            sb.append("]");
        }
        return sb.toString();
    }
}
